package com.gp.ajax.app2.com.gp.ajax.app2.beans;

import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {
    //存放ShoppingCartItem 的Map;键：书名，值：ShoppingCartItem对象
    private Map<String, ShoppingCartItem> items = new HashMap<>();
    private String bookName = "";

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void addToCart(String bookNaME, double price) {
        this.bookName = bookNaME;

        if (items.containsKey(bookNaME)) {
            ShoppingCartItem temp = items.get(bookNaME);
            temp.setNum(temp.getNum() + 1);
        } else {
            ShoppingCartItem temp = new ShoppingCartItem();
            temp.setNum(1);
            temp.setBookName(bookNaME);
            temp.setPrice(price);
            items.put(bookNaME, temp);
        }
    }

    public int getTotalBookNumer() {
        int nums = 0;
        for (ShoppingCartItem temp : items.values()) {
            nums += temp.getNum();
        }
        return nums;
    }

    public double getTotalMoney() {
        double values = 0;
        for (ShoppingCartItem temp : items.values()) {
            values += (temp.getPrice() * temp.getNum());
        }
        return values;
    }
}
